package system.base;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import system.SelectManager;
import system.data.WarehouseCategoryBase;
import framework.core.BizException;
import framework.core.Conditions;
import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBPreparedStatement;
import framework.core.DBResult;
import framework.core.DateUtil;
import framework.core.ObjectList;
import java.util.*;

import com.hitrust.gs.lib.GSAutoAuditongDateConfigurator;

/*
 Author    :chengbiao.he
 CreateTime:2008-02-03 14:56:55
 */
public class WarehouseCategory extends WarehouseCategoryBase {

    // ----system generated start----//
    public WarehouseCategory() {

    }

    private static void delete(DBConnection con, String id) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(ID, id);

        delete(con, TABLE_NAME, conditions);
    }

    public static WarehouseCategory fetch() {
        return new WarehouseCategory();
    }

    public static WarehouseCategory copy(WarehouseCategory srcObj) {
        WarehouseCategory obj = fetch();
        obj.copyBase(srcObj);
        return obj;
    }

    public static WarehouseCategory fetch(DBConnection con, String id) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(ID, id);

        return fetch(con, conditions);
    }

    public static WarehouseCategory fetch(DBConnection con, Conditions conditions) throws Exception {
        DBResult result = null;
        try {
            result = DBEngine.doSelect(con, "select * from " + TABLE_NAME, conditions);
            java.sql.ResultSet rs = result.getRs();
            WarehouseCategory obj = null;
            if (rs.next()) {
                obj = fetch(rs);
            }
            return obj;
        } finally {
            if (result != null)
                result.clear();
        }
    }

    public static WarehouseCategory fetch(java.sql.ResultSet rs) throws Exception {
        WarehouseCategory obj = new WarehouseCategory();
        obj.initObject(rs);
        return obj;
    }

    protected void getUnique(DBConnection con) throws Exception {
        setId(DBEngine.getId(con));
    }

    // ----system generated end----//

    // ----add by chengbiao.he start----//
    public void canUpdateWarehouseCategory(DBConnection con, String currWareCode) throws Exception {

        /*
         * if(ConstDefine.STR_YES.equals(getIsNormal())){ throw new
         * BizException("当前是正式产品不能修改"); }
         */
        if (!getWarehouseCode().equals(currWareCode)) {
            throw new BizException("所选产品不属于本仓库产品不能修改");
        }
    }

    // 取得所属分类名称

    public String viewParentCategoryName(DBConnection con) throws Exception {
        Conditions cs = new Conditions();
        cs.add(CATEGORY_CODE, getParentCategoryCode());
        WarehouseCategory wc = WarehouseCategory.fetch(con, cs);
        if (wc == null)
            return "";
        else
            return wc.viewCategoryName(con);
    }

    // 通过仓库分类关联仓库产品分类关联表取得产品对象列表
    public static ObjectList listByWarehouseCategoryReLation(DBConnection con, String warehouseCode, String categoryCode, ProductSearchEvent event)
            throws Exception {

        Conditions cs = event.toConditions();
        String[] parms = new String[] { warehouseCode, categoryCode };
        cs.add(Product.CONCENTRICITY_CODE + " in (select product_code from t_warehouse_category_relation where warehouse_code=? and category_code=?)",
                Conditions.CUSTOM, parms);

        return Product.fetch().search(con, cs);

    }

    public String viewStatus(DBConnection con) throws Exception {
        return SelectManager.viewYesOrNo(getStatus() + "");
    }

    /*
     * public static WarehouseCategory fetchbyCode(DBConnection con,String
     * categoryCode,String warehouseCode) throws Exception { Conditions cs = new
     * Conditions(); cs.add(WarehouseCategory.CATEGORY_CODE,categoryCode);
     * cs.add(WarehouseCategory.WAREHOUSE_CODE,warehouseCode); return
     * WarehouseCategory.fetch(con, cs); }
     */
    // ----add by chengbiao.he end----//
    private void checkUnique(DBConnection con) throws Exception {
        Conditions cs = new Conditions();
        cs.add(CATEGORY_CODE, getCategoryCode());
        cs.add(ID, Conditions.NOT_EQUAL, getId());
        if (DBEngine.doCount(con, "select count(id) from " + TABLE_NAME, cs) > 0) {
            setId("");
            throw new BizException("已存在的编码，请重新填写。");
        }
    }

    public void beforeUpdate(DBConnection con) throws Exception {
        checkUnique(con);
        setLastUpdateTime(new DateUtil().getFullString());
    }

    public void beforeInsert(DBConnection con) throws Exception {
        checkUnique(con);
        DateUtil du = new DateUtil();
        setCreateTime(du.getFullString());
        setLastUpdateTime(du.getFullString());
    }

    //add by Sindy start
    public static WarehouseCategory fetchbyCode(DBConnection con, String code, String warehouseCode) throws Exception {
        WarehouseCategory wc = null;
        Conditions cs = new Conditions();
        cs.add(CATEGORY_CODE, code);
        cs.add(WAREHOUSE_CODE, warehouseCode);
        wc = WarehouseCategory.fetch(con, cs);
        if (wc == null)
            return null;
        else
            return wc;
    }
    //add by Sindy end
    public static WarehouseCategory fetchbyLikeCode(DBConnection con, String code, String warehouseCode) throws Exception {
        WarehouseCategory wc = null;
        Conditions cs = new Conditions();
        cs.add(CATEGORY_CODE, code);
        String  supperWarehouseCode =  warehouseCode.trim().length()>6? warehouseCode.trim().substring(0, warehouseCode.length()-2):warehouseCode;
        //判断是否是共享数据的仓库编码
        GSAutoAuditongDateConfigurator.getInstance();
        if(!GSAutoAuditongDateConfigurator.isShareDatesWarehouses(supperWarehouseCode)){
        	cs.add(WAREHOUSE_CODE, warehouseCode);
        }else{
        	cs.add(WAREHOUSE_CODE,Conditions.LIKE, supperWarehouseCode+"%");
        }

        
        wc = WarehouseCategory.fetch(con, cs);
        if (wc == null)
            return null;
        else
            return wc;
    }
    public static List getWarehouseCodeByParentCode(String warehouseCode){
        String sql = "select warehouse_code,warehouse_name from t_warehouse_category group by warehouse_code,warehouse_name";
        ResultSet rs = null;
        DBPreparedStatement ps = null;
        List list = new ArrayList();
        DBConnection con = null; 
        try {
            con = DBConnection.open(false);
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                String[] str = {rs.getString("warehouse_code"),rs.getString("warehouse_name")};
                list.add(str);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {if(rs != null) rs.close();} catch (SQLException e) {e.printStackTrace();}
            try { if(ps != null) ps.close();} catch (Exception e) {e.printStackTrace(); }
            try {if(con != null) con.close();} catch (Exception e) {e.printStackTrace();}
        }
        return list;
    }
    public static List getWarehouseCode(String warehouseCode){
        String sql = "select code,name from t_warehouse where code like '"+warehouseCode+"%' ";
        ResultSet rs = null;;
        DBPreparedStatement ps = null;
        List list = new ArrayList();
        DBConnection con = null; 
        try {
            con = DBConnection.open(false);
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                String[] str = {rs.getString("code"),rs.getString("name")};
                list.add(str);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {if(rs != null) rs.close();} catch (SQLException e) {e.printStackTrace();}
            try { if(ps != null) ps.close();} catch (Exception e) {e.printStackTrace(); }
            try {if(con != null) con.close();} catch (Exception e) {e.printStackTrace();}
        }
        return list;
    }
}
